<?php
/**
 * @title 2976. 转换字符串的最小成本 I - Flyod算法
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String $source
     * @param String $target
     * @param String[] $original
     * @param String[] $changed
     * @param Integer[] $cost
     * @return Integer
     */
    function minimumCost($source, $target, $original, $changed, $cost) {
        /**
         * @since 2024-01-15 Flyod算法
         */
        $minCostArray = [];
        for($i=count($cost)-1; $i>=0; $i--){
            /**
             * @since 2024-01-15 字符转换from => to, 成本cost
             */
            $from = $original[$i];
            $to = $changed[$i];

            /**
             * @since 2024-01-15 取最小值
             * 注意，可能存在下标 i 、j 使得 original[j] == original[i] 且 changed[j] == changed[i]
             */
            if(isset($minCostArray[$from][$to])){
                $minCostArray[$from][$to] = min($cost[$i], $minCostArray[$from][$to]);
            } else {
                $minCostArray[$from][$to] = $cost[$i];
            }
        }

        /**
         * @since 2024-01-15 三层循环
         */
        $charString = "abcdefghijklmnopqrstuvwxyz";
        for($k=0; $k<26; $k++){
            $mid = $charString[$k];
            for($i=0; $i<26; $i++){
                $left = $charString[$i];
                for($j=0; $j<26; $j++){
                    $right = $charString[$j];

                    /**
                     * @since 2024-01-15 left和right不能相同
                     */
                    if($left == $right){
                        continue;
                    } else {}

                    /**
                     * @since 2024-01-15 [left, mid]和[mid, right]存在转换
                     */
                    if(isset($minCostArray[$left][$mid]) AND isset($minCostArray[$mid][$right])){
                        if(isset($minCostArray[$left][$right])){
                            $minCostArray[$left][$right] = min($minCostArray[$left][$right], $minCostArray[$left][$mid] + $minCostArray[$mid][$right]);
                        } else {
                            $minCostArray[$left][$right] = $minCostArray[$left][$mid] + $minCostArray[$mid][$right];
                        }
                    } else {}
                }
            }
        }
        // return $minCostArray;

        /**
         * @since 2024-01-15 最小代价
         */
        $minCost = 0;
        for($i=0; $i<strlen($source); $i++){
            if($source[$i] != $target[$i]){
                if(isset($minCostArray[$source[$i]][$target[$i]])){
                    $minCost += $minCostArray[$source[$i]][$target[$i]];
                } else {
                    return -1;
                }
            } else {}
        }

        /**
         * @return Integer
         */
        return $minCost;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.15_4.php
 */
$datas = [
    ["abcd", "acbe", ["a","b","c","c","e","d"], ["b","c","b","e","b","e"], [2,5,5,1,2,20]], // 28
    ["aaaa", "bbbb", ["a","c"], ["c","b"], [1,2]], // 12
    ["abcd", "abce", ["a"], ["e"], [10000]], // -1
    ["bcaabaddac", "bdccbdaadc", ["c","d","a","a","c","a","d"], ["a","a","d","b","d","c","c"], [4,3,6,3,11,6,4]], // 40
    ["aaadbdcdac", "cdbabaddba", ["a","c","b","d","b","a","c"], ["c","a","d","b","c","b","d"], [7,2,1,3,6,1,7]], // 39
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minimumCost(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
